﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;
using 纺织贸易管理系统.其他窗体;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.基本资料
{
    public partial class 原料信息 : Form
    {
        public 原料信息()
        {
            InitializeComponent();
            CreateGrid.Create(this.Name, gridView1);
            Query();
        }

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = new RawMaterial() };
            fm.ShowDialog();
        }

        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "供货商资料");
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
            this.gridControl1.DataSource = Connect.GetSugar().Queryable<RawMaterial>().Where(x => x.ProductName.Contains (txtmingcheng.Text) && x.AuxiliaryEncoding.Contains(txtzhujici.Text)).ToList();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {         
                var fm = new 新增原料() { Useful = FormUseful.修改, Raw = Connect.CreatConnect().QueryOneResult<RawMaterial>(x=>x.ID==(int) gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID"))};
                fm.ShowDialog();
                Query();          
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 新增原料() { Useful = FormUseful.新增, Raw =new RawMaterial() };
            fm.ShowDialog();
            Query();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {      
                try
                {
                    var res = MessageBox.Show($"您确定要删除该编号{gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID")}的原料信息吗？", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (res == DialogResult.Yes)
                    {
                    
                        Connect.GetSugar ().Deleteable <RawMaterial>().Where (x => x.ID ==(int) gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID")).ExecuteCommand();
                        AlterDlg.Show("删除成功！");
                        Query();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除时发生错误！" + ex.Message);
                }
            }
    }
}
